package com.lottery.im.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.util.ScreenUtils;

/**
 * @TODO 检测更新工具
 */
public class CheckUpdateDialog {
  private static Dialog mUpdateDialog;

  /**
   * @TODO 提示更新窗口
   */
  public static void create(Context context, final String versionName, final String url,
      String content) {
    if (mUpdateDialog != null) return;
    mUpdateDialog = new Dialog(context, R.style.CustomMainDialog);
    Window window = mUpdateDialog.getWindow();
    window.requestFeature(Window.FEATURE_NO_TITLE);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    mUpdateDialog.setContentView(R.layout.dialog_main_update_picker);
    window.setGravity(Gravity.CENTER);
    window.setWindowAnimations(R.style.center_animation);
    WindowManager.LayoutParams params = window.getAttributes();
    params.width = ScreenUtils.getWidth(context);
    window.setAttributes(params);
    TextView mTitle = mUpdateDialog.findViewById(R.id.title_picker);
    WebView mWebView = mUpdateDialog.findViewById(R.id.content_picker);
    mTitle.setText("发现新版本  V" + versionName);
    WebSettings settings = mWebView.getSettings();
    settings.setDefaultTextEncodingName("utf-8");
    settings.setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new HelloWebViewClient());
    mWebView.setBackgroundColor(0);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    String css = "<style type=\"text/css\"> img {" + "width:100%;" +//限定图片宽度填充屏幕
        "height:auto;" +//限定图片高度自动
        "}" + "body {" + "margin-right:0px;" +//限定网页中的文字右边距为15px(可根据实际需要进行行管屏幕适配操作)
        "margin-left:0px;" +//限定网页中的文字左边距为15px(可根据实际需要进行行管屏幕适配操作)
        "margin-top:0px;" +//限定网页中的文字上边距为15px(可根据实际需要进行行管屏幕适配操作)
        "margin-bottom:0px;" + "font-size:13px;color:#333333;" +//限定网页中文字的大小为15px,请务必根据各种屏幕分辨率进行适配更改
        "word-break:break-all;" +//允许自动换行(汉字网页应该不需要这一属性,这个用来强制英文单词换行,类似于word/wps中的西文换行)
        "}" + "</style>";
    String html = "<html><header>" + css + "</header>" + content + "</html>";
    mWebView.loadDataWithBaseURL("about:blank", html, "text/html", "utf-8", null);
    mUpdateDialog.show();
    mUpdateDialog.setOnKeyListener((dialog, keyCode, event) -> true);
    ImageView mSure = mUpdateDialog.findViewById(R.id.sure_picker);
    mSure.setOnClickListener(v -> {
      //开始下载
      Intent intent = new Intent();
      intent.setAction("android.intent.action.VIEW");
      Uri content_url = Uri.parse(url);
      intent.setData(content_url);
      context.startActivity(intent);
      mUpdateDialog.dismiss();
      mUpdateDialog = null;
    });
  }

  //Web视图
  private static class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      view.loadUrl(url);
      return true;
    }
  }
}
